﻿using EnglishAuction.BUS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EnglishAuction
{
    public partial class ManageProduct : System.Web.UI.Page
    {
        private void fillData()
        {
            gvQuanLySanPham.DataSource = SanPhamBUS.GetProductByStatus(1, -1);
            gvQuanLySanPham.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<LoaiSanPham> sanPhams = LoaiSanPhamBUS.GetAll();
                sanPhams.Insert(0, new LoaiSanPham { Id = -1, TenLoaiSanPham = "Tất cả loại sản phẩm"});
                cbbLoaiSanPham.DataSource = sanPhams;
                cbbLoaiSanPham.DataTextField = "TenLoaiSanPham";
                cbbLoaiSanPham.DataValueField = "Id";
                cbbLoaiSanPham.DataBind();

                fillData();
            }
        }

        protected void gvQuanLySanPham_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem == null)
                return;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label type = (Label)e.Row.FindControl("lbNguoiDung");
                type.Text = NguoiDungBUS.GetById(int.Parse(type.Text)).TaiKhoan;
            }
        }

        protected void gvQuanLySanPham_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //gvQuanLySanPham.PageIndex = e.NewPageIndex;
            //fillData();
        }

        protected void gvQuanLySanPham_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditProduct" || e.CommandName == "OptionProduct")
            {               
                Response.Redirect("~/EditProduct.aspx?ID="+e.CommandArgument);
            }
            if (e.CommandName == "DeleteProduct")
            {
                Response.Redirect("~/DeleteProduct.aspx?ID=" + e.CommandArgument);
            }

        }

        protected void cbbLoaiSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbLoaiSanPham.SelectedIndex < 0)
                return;
            int id = int.Parse(cbbLoaiSanPham.SelectedValue);
            gvQuanLySanPham.DataSource = SanPhamBUS.GetProductByStatus(1, id);
            gvQuanLySanPham.DataBind();
        }
    }
}